/*! \file dyncorec.cpp

Date of creation: 2010-15-11

Copyright
Julian David Colorado, jd.colorado@upm.es

*/

#include "dynlib.h"

/*!
Dynamic simulation core.\n\n
Input Files:\n
 simparam.txt\n
 Xini.txt\n\n
Output Files:\n
 X.txt\n
 u.txt\n
*/
int main()
{

	
	Matrix SimParam(4,6);
	
	int i,j,pt,k;
	char linea[13];
	FILE *fp,*f1;
	
	//SimParam input
	fp = fopen("simparam.txt","r+"); /*!< File pointer of simparam.txt */
	
	fseek (fp,0,SEEK_SET );
	for(i=1;i<=4;i++)
	{
		for(j=1;j<=6;j++)
		{
			fscanf(fp,"%s",&linea);
			SimParam(i,j)=atof(linea);
		}
	}
	fclose(fp);
	
	pt=int(SimParam(3,2)/SimParam(3,3))+1;
	k=int(SimParam(4,4)+SimParam(4,3));
	
	Matrix X(k,pt), u(1,pt);
	
	//input matrix
	f1 = fopen("Xini.txt","r+");
	
	//fseek (fp,0,SEEK_SET );
	for(i=1;i<=(SimParam(4,4)+SimParam(4,3));i++)
	{
		fscanf(f1,"%s",&linea);
		X(i,1)=atof(linea);
		
	}
	fclose(f1);
	
	/*if ( (fp = fopen("u.txt","r+")) == NULL )
		error(E_EOF,"my_function");
	m_finput(fp,u);
	fclose(fp);*/
	
	//Dynamic loop
	for(i=1;i<=pt-1;i++)
	{
		odesolver(SimParam,X,i,u);
	}	
	ofstream f2;
	f2.open ("X.txt");
	f2 << X;
	f2.close();
	
	f2.open ("u.txt");
	f2 << u;
	f2.close();
	
	return 0;
}
